//
//  WYJVisitorLoginView.swift
//  MySinaWeibo
//
//  Created by eastriver on 16/10/5.
//  Copyright © 2016年 believewilliam. All rights reserved.
//

import UIKit
import SnapKit
@objc protocol WYJVisitorLoginViewDelegate: NSObjectProtocol {
    // 登录
    func userWillLogin()
    // 注册
    @objc optional func userWillRegister()
}
class WYJVisitorLoginView: UIView {
    
    // 代理属性
    weak var delegate: WYJVisitorLoginViewDelegate?
    // 懒加载子控件
    // 转圈的view
    private lazy var circleImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    // 房子
    private lazy var iconImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    // 遮罩
    private lazy var backImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    // 提示文字
    private lazy var tipLabel: UILabel = {
        let lab = UILabel(title: "关注一些人，快回到这里看看有什么，你会发现很多乐趣和志同道合的朋友", textColor: UIColor.darkGray, fontSize: 14)
        lab.preferredMaxLayoutWidth = 224
        lab.numberOfLines = 0
        lab.textAlignment = .center
        return lab
    }()
    // 注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton(title: "注册", textColor: UIColor.darkGray, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    // 登录按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton(title: "登录", textColor: UIColor.orange, fontSize: 14)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 设置UI
    private func setupUI() {
        addSubview(circleImageView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        circleImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(circleImageView.snp.bottom).offset(16)
            make.centerX.equalTo(circleImageView)
            
        }
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(18)
            make.width.equalTo(100)
        }
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(18)
            make.width.equalTo(100)
        }
        // 设置背景颜色
        let value = CGFloat(237 / 255.0)
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        // 添加点击事件
        registerBtn.addTarget(self, action: #selector(regiserBtnDidClick), for: .touchUpInside)
        loginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
    }
    
    func regiserBtnDidClick() {
        self.delegate?.userWillRegister?()
    }
    func loginBtnDidClick() {
        self.delegate?.userWillLogin()
    }
    // 更新UI函数
    func updateInfo(tipText: String, imageName: String?) {
        if imageName == nil {
            // 是首页视图
            // 添加遮罩与大房子
            addSubview(backImageView)
            backImageView.snp.makeConstraints { (make) in
                make.center.equalTo(circleImageView)
            }
            addSubview(iconImageView)
            iconImageView.snp.makeConstraints { (make) in
                make.center.equalTo(circleImageView)
            }
            // 使圈圈转动起来
            startAnimation()
            
        }else {
            self.tipLabel.text = tipText
            self.circleImageView.image = UIImage(named: imageName!)
            
        }
    }
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 设置有关参数
        animation.repeatCount = MAXFLOAT
        animation.toValue = M_PI * 2
        animation.duration = 15
        animation.isRemovedOnCompletion = false
        // 添加到circleImageView的图层上
        circleImageView.layer.add(animation, forKey: nil)
    }
}
